<?php

/**
 * system actions.
 *
 * @package    sf_sandbox
 * @subpackage system
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 2692 2006-11-15 21:03:55Z fabien $
 */
class systemActions extends sfActions
{
    /**
    * Executes index action
    *
    */
    public function executeIndex()
    {
        $this->redirect('@homepage');
    }

	public function executeWelcome() 
	{
	    
	}

    public function executeLogin()
    {
        $this->forward('user', 'login');
    }

    public function executeSetlng()
    {
        $lng = User::getValidCultureOrDefault($this->getRequestParameter('lng'));
        //debugvar('Culture to set is: ' . $lng);
        //exit();
        $this->getUser()->setCulture($lng);
        //debugvar($this->getRequest()->getHost());
        //debugvar($this->getRequest()->getScriptName());
        //exit('exitting..');

        //debugvar('Referer: ' . $this->getRequest()->getReferer());
        //$this->redirect($this->getRequest()->getReferer());
        if( $this->hasFlash('returnUrl') )
        {
            $this->redirect($this->getFlash('returnUrl'));
        }
        $this->redirect('@homepage');
    }
    
    public function executeSetcurrency()
    {
    	if( $currency = $this->getRequestParameter('currency', 'LTL') )
    	{
    	   $this->getUser()->setAttribute('currency', $currency);
    	}
    	if( $this->hasFlash('returnUrl') )
        {
            $this->redirect($this->getFlash('returnUrl'));
        }
        $this->redirect('@homepage');
    }

    /**
     * Clears current environment cache
     */
    public function executeCc() 
    {
        $cacheDir = sfConfig::get('sf_base_cache_dir');
//        debugvar('Cache dir: ' . $cacheDir);
        if( file_exists($cacheDir) ) 
        {
            function rmdir_recursive($dir) 
            {
                echo 'Deleting: ' . $dir . '...<br />';
            	$files = scandir($dir);
                array_shift($files);
                array_shift($files);
               
                foreach ($files as $file) 
                {
                    $file = $dir . '/' . $file;
                    if (is_dir($file)) 
                    {
                        rmdir_recursive($file);
                        @rmdir($file);
                    } 
                    else 
                    {
                        @unlink($file);
                    }
                }
                @rmdir($dir);
            }
            rmdir_recursive($cacheDir);
        }

        // Do not redirect to any page, to prevent recreation of cache
        exit('done...');
    }

	public function execute401() 
	{
	    
	}
	
    public function executeError404() 
    {
        $url = substr($this->getRequest()->getPathInfo(), 4);
//        debugvar('URL to search: ' . $url);
        $c = new Criteria();
        $c->add(SeoPeer::URL, $url);
        $c->addDescendingOrderByColumn(SeoPeer::ID);
        $seo = SeoPeer::doSelectOne($c);
//        debugvar($seo);
        if( $seo )
        {
            $this->getRequest()->setParameter('id', $seo->getObjId());
            if($seo->getCulture())
            {
                $this->getUser()->setCulture($seo->getCulture());
            }
            $this->getResponse()->setStatusCode(200);
           	$this->getResponse()->setHttpHeader('Status', '200 OK');
            $this->forward($seo->getObjType(), $seo->getObjAction());
            
        }
    }
	

}
